package com.ruyuan.commodity.controller.seller;

import com.ruyuan.commodity.model.goods.dto.GoodsQueryParam;
import com.ruyuan.commodity.model.goods.vo.CacheGoods;
import com.ruyuan.commodity.model.goods.vo.GoodsSkuVO;
import com.ruyuan.commodity.open.api.annotation.Auth;
import com.ruyuan.commodity.open.api.model.common.WebPage;
import com.ruyuan.commodity.common.mamager.GoodsInfoQueryManager;
import com.ruyuan.commodity.common.mamager.GoodsSkuManager;
import com.ruyuan.commodity.open.api.aspect.UserInfo;
import com.ruyuan.commodity.open.api.util.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import java.util.List;

/**
 * 商品sku控制器
 */
@RestController
@RequestMapping("/seller/goods")
@Api(value = "商品sku相关API")
public class GoodsSkuController {

	/**
	 * 商品查询类业务管理器
	 */
	@Autowired
	private GoodsInfoQueryManager goodsInfoQueryManager;
	/**
	 * 商品SKU业务管理器
	 */
	@Autowired
	private GoodsSkuManager goodsSkuManager;

	@Auth
	@ApiOperation(value = "商品sku信息获取api")
	@ApiImplicitParam(name	= "goodsId", value = "商品id",	required = true, dataType = "int",	paramType =	"path")
	@GetMapping("/{goodsId}/skus")
	public List<GoodsSkuVO> queryByGoodsId(@PathVariable(name = "goodsId") Long goodsId) {

		CacheGoods goods = goodsInfoQueryManager.getFromCache(goodsId);

		return  goods.getSkuList();
	}

	@Auth
	@ApiOperation(value = "更新商品的sku信息")
	@PutMapping("/{goodsId}/skus")
	public List<GoodsSkuVO> updateGoodsSku(@PathVariable(name = "goodsId") Long goodsId, @RequestBody List<GoodsSkuVO> skuList) {

		goodsSkuManager.editSkus(skuList, goodsId);

		return  skuList;
	}

	@Auth
	@GetMapping(value = "/skus/{skuIds}/details")
	@ApiOperation(value = "查询多个商品的基本信息")
	public List<GoodsSkuVO> getGoodsDetail(@PathVariable("skuIds") Long[] skuIds) {
		return this.goodsSkuManager.query(skuIds);
	}

	@Auth
	@ApiOperation(value = "查询SKU列表", response = GoodsSkuVO.class)
	@GetMapping("/skus")
	public WebPage list(GoodsQueryParam param, @ApiIgnore Long pageNo, @ApiIgnore Long pageSize) {
		param.setPageNo(pageNo);
		param.setPageSize(pageSize);
		UserInfo userInfo = AuthContextUtil.getUserInfo();
		param.setSellerId(userInfo.getSellerId());
		return this.goodsSkuManager.list(param);
	}

}
